﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCSuaNCC : UserControl
    {
        String id_NCC;
        String idNhaCungCap;
        NhaCungCapBUO nccBUO = new NhaCungCapBUO();
        public UCSuaNCC(String id)
        {
            InitializeComponent();
            this.id_NCC = id;
            lblIDNCC.Text = id_NCC;
            showData();
            dataGridView1.DataSource = nccBUO.GetAll();
            txtTimTheoMaSoNCC.Enabled = false;
            txtTimTheoNCC.Enabled = false;
        }
        /// <summary>
        /// Hiển thị dữ liệu cần sửa
        /// </summary>
        void showData()
        {
            var list = nccBUO.GetSingle(id_NCC);
            txtNCC.Text = list.TenNhaCungCap;
            txtDiaChi.Text = list.DiaChi;
            txtDienThoai.Text = list.SoDienThoai;
            txtEmail.Text = list.Email;
        }
        /// <summary>
        /// Hiển thị dữ liệu khi click vào datagridview
        /// </summary>
        void binding()
        {
            txtNCC.DataBindings.Clear();
            txtNCC.DataBindings.Add("Text", dataGridView1.DataSource,"TenNhaCungCap");
            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", dataGridView1.DataSource, "Email");
            txtDienThoai.DataBindings.Clear();
            txtDienThoai.DataBindings.Add("Text", dataGridView1.DataSource, "SoDienThoai");
            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", dataGridView1.DataSource, "DiaChi");
        }
        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtNCC.Text = "";
        }

        private void rdTimTheoTenKhach_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaSoNCC.Enabled = false;
            txtTimTheoNCC.Enabled = true;
            txtTimTheoMaSoNCC.Text = "";
            dataGridView1.Enabled = true;
            binding();
        }

        private void radioTimTheoEmail_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void rdAll_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaSoNCC.Enabled = false;
            txtTimTheoNCC.Enabled = false;
            txtTimTheoMaSoNCC.Text = "";
            txtTimTheoNCC.Text = "";
            dataGridView1.Enabled = false;
            dataGridView1.DataSource = nccBUO.GetAll();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tbl_NhaCungCap tblNCC = new tbl_NhaCungCap();
            tblNCC.TenNhaCungCap = txtNCC.Text;
            tblNCC.DiaChi = txtDiaChi.Text;
            tblNCC.Email = txtEmail.Text;
            tblNCC.SoDienThoai = txtDienThoai.Text;
            if (nccBUO.Update(id_NCC,tblNCC))
            {
                dataGridView1.DataSource = nccBUO.GetAll();
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dataGridView1.DataSource = nccBUO.GetAll();
                MessageBox.Show("Cập nhật dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            binding();
        }

        private void txtTimTheoNCC_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nccBUO.SearchByTenNCC(txtTimTheoNCC.Text.Trim());
        }

        private void rdTimTheoMaSoNCC_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaSoNCC.Enabled = true;
            txtTimTheoNCC.Enabled = false;
            txtTimTheoNCC.Text = "";
            dataGridView1.Enabled = true;
            binding();
        }
        //Tìm kiếm theo mã số nhà cung cấp
        private void txtTimTheoMaSoNCC_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nccBUO.SearchByMaNCC(txtTimTheoMaSoNCC.Text.Trim());
        }

        private void thêmNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCThemNCC ucThemNCC=new UCThemNCC();
            ControlAccess.ShowConTrol(form, ucThemNCC, "grbHienThiChucNang");
        }
        /// <summary>
        /// Lấy id của nhà cung cấp khi click vào datagridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        idNhaCungCap = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xóaNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (nccBUO.Delete(idNhaCungCap))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = nccBUO.GetAll();
            }
        }
            

    }
}
